<?php

namespace app\admin\controller;

use app\common\controller\AdminController;

//引入外部函数
use app\common\model\hfztcy\LibMessage;
use common\Common;
use think\Db;


/**
 * 留言内容
 * @name Message.php
 * @package hfztcy
 * @message controller
 * @author rxy
 * @version 1.0
 * @copyright CHANGE INC
 * @since 2020-6-5
 */
class Message extends AdminController
{
    protected $model;

    /**
     * 构造函数
     */
    public function __construct()
    {
        $this->model = new LibMessage();
        $this->rightVerify(session('admin'), url("admin/Login/login"));
        parent::__construct();
    }

    /**
     * 列表页
     */
    function messageList()
    {
        $this->getSetMenu($this);
        $this->log(__CLASS__, __FUNCTION__, "列表页", 1, 'view');
        return $this->fetch('Message/messageList/messageList');
    }

    /**
     * 添加页面
     */
    function addMessage()
    {
        $this->getMenu($this);
        $this->log(__CLASS__, __FUNCTION__, "添加页面", 1, 'view');
        return $this->fetch('Message/addMessage/addMessage');
    }

    /**
     * 添加
     */
    function insertMessage()
    {
        $messageInfo = $this->getArgsList($this, array('title', 'content'), false);
        $res = $this->model->findMessage(['title' => $messageInfo['title']]);
        if ($res['errorCode'] == 0) {
            echo json_encode(Common::errorArray(1, "标题已存在", false));
            die;
        }
        $verifyRes = $this->model->goCheck($messageInfo);
        if ($verifyRes['errorCode'] == 0) {
            $messageInfo['add_time'] = Common::getTime();
            $result = $this->model->addMessage($messageInfo);
            if ($result['errorCode'] == 1) {
                echo json_encode(Common::errorArray(1, $result['errorInfo'], false));
                die;
            }
        } else {
            echo json_encode(Common::errorArray(1, $verifyRes['errorInfo'], false));
            die;
        }

        $this->log(__CLASS__, __FUNCTION__, "添加", 0, 'add');
        echo json_encode($result);
    }

    /**
     * 编辑页面
     */
    function editMessage()
    {
        $this->getMenu($this);
        //$messageInfo = $this->model->findMessage(['id' => input("id")]);
        $id = input('id');
        $sql = <<< SQL
select a.id,a.user_id,a.content,a.reply_content,a.reply_time,b.username,c.nick_name 
from `hfztcy_message` as a 
    left join `hfztcy_user` as b 
        on a.user_id = b.user_id 
    left join `base_user` as c 
        on a.user_id = c.id 
where a.id = {$id}
SQL;
        $query = Db::query($sql);
        $this->assign('messageInfo', $query[0]);
        $this->log(__CLASS__, __FUNCTION__, "编辑页面", 1, 'view');
        return $this->fetch('Message/editMessage/editMessage');
    }

    /**
     * 更新
     */
    function updateMessage()
    {
        $messageInfo = $this->getArgsList($this, array('id', 'reply_content'), false);
        $id = $messageInfo['id'];
        $verifyRes = $this->model->goCheck($messageInfo, 'edit');
        if ($verifyRes['errorCode'] == 0) {
            $messageInfo['reply_user_id'] = session('admin.id');
            $messageInfo['reply_time'] = Common::getTime();
            $result = $this->model->updateMessage(array("id" => $id), $messageInfo);
            if ($result['errorCode'] == 1) {
                echo json_encode(Common::errorArray(1, $result['errorInfo'], false));
                die;
            }
        } else {
            echo json_encode(Common::errorArray(1, $verifyRes['errorInfo'], false));
            die;
        }

        $this->log(__CLASS__, __FUNCTION__, "更新", 0, 'update');
        echo json_encode($result);
    }

    /**
     * 删除
     */
    function deleteMessage()
    {
        $id = input("id");
        $messageRes = $this->model->findMessage(['id' => $id]);
        if ($messageRes['errorCode'] == 0) {
            $result = $this->model->deleteMessage($id);
            echo json_encode($result);
            die();
        }
        echo json_encode(Common::errorArray(1, '不存在', false));
        die();
    }

    /**
     * 分页查询
     */
    function pagingMessage()
    {
        $args = $this->getArgsList($this, ['username']);
        $page = $this->getPageInfo();
        $where = [];
        if (!empty($args['username'])) $where[] = "`username` like '%" . $args['username'] . "%'";
        $result = $this->model->pagingMessage($page, implode(' AND ', $where));
        echo json_encode($result);
    }
}

?>